package cn.ljq.controller.adm;

import cn.ljq.dao.ClzMapper;
import cn.ljq.dao.CourseMapper;
import cn.ljq.dao.TaskMapper;
import cn.ljq.dao.UserMapper;
import cn.ljq.entity.Clz;
import cn.ljq.entity.Course;
import cn.ljq.entity.Task;
import cn.ljq.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;

@Controller
@RequestMapping("/adm/task")
public class AdmTaskController {

    @Autowired
    TaskMapper taskMapper;

    @Autowired
    ClzMapper clzMapper;

    @Autowired
    CourseMapper courseMapper;

    @Autowired
    UserMapper userMapper;

    @RequestMapping("/show")
    public String findAll(Model model){


        List<Task> tasks = taskMapper.findAll();  //查询所有教师所带班级和所教课程

        List<User> allTea = userMapper.findByRole("tea");  //查询所有教师的名字 没有重复

        List<Clz> allClz = clzMapper.findAll();  //查询所有班级名称


        List<Course> allCourse = courseMapper.findAll();  //查询所有课程名称

        model.addAttribute("tasks",tasks);

        model.addAttribute("allTea",allTea);

        model.addAttribute("allClz",allClz);

        model.addAttribute("allCourse",allCourse);

        return "adm/task/page";
    }


    @GetMapping("/go2add")
    public String go2add(Model model){

        model.addAttribute("actmsg","添加排课");

        model.addAttribute("action","add");

        model.addAttribute("tasks",new Task());

        return "forward:/adm/task/show";
    }

    @PostMapping("/add")
    public String add(Model model,Task task){

        taskMapper.add(task);

        return "forward:/adm/task/show";
    }




    @GetMapping("/del/{kid}")
    public String del(Task task){

        taskMapper.del(task.getKid());

        return "forward:/adm/task/show";
    }

}
